package com.wang.user.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.exception.BusinessException;
import com.wang.param.ShopQueryParam;
import com.wang.reponse.ShopVo;
import com.wang.user.model.Shop;
import com.wang.user.model.ShopCollection;
import com.wang.user.model.User;
import com.wang.user.service.ShopCollectionService;
import com.wang.user.mapper.ShopCollectionMapper;
import com.wang.user.service.ShopService;
import com.wang.user.service.user.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author bkty0
* @description 针对表【shop_collection(商铺收藏表)】的数据库操作Service实现
* @createDate 2025-04-23 10:39:21
*/
@Service
public class ShopCollectionServiceImpl extends ServiceImpl<ShopCollectionMapper, ShopCollection>
    implements ShopCollectionService{

    @Resource
    private ShopCollectionMapper shopCollectionMapper;

    @Resource
    private UserService userService;

    @Resource
    private ShopService shopService;

    @Override
    public List<ShopVo> listShopCollection(ShopQueryParam queryParam) {
        User loginUser = userService.getLoginUser();
        queryParam.setUid(loginUser.getUid());
        LambdaQueryWrapper<ShopCollection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopCollection::getUserId, loginUser.getUid());
        List<ShopCollection> shopCollections = list(queryWrapper);
        if(CollUtil.isEmpty(shopCollections)){
            return Collections.emptyList();
        }
        List<Long> shopIds = shopCollections.stream().map(ShopCollection::getShopId).distinct().collect(Collectors.toList());
        return shopService.listVoByShopIds(shopIds);
    }

    @Override
    public boolean collection(ShopCollection shopCollection) {
        Shop shopById = shopService.getById(shopCollection.getShopId());
        if(shopById ==null){
            throw new BusinessException("商铺信息不存在");
        }
        User loginUser = userService.getLoginUser();
        ShopCollection oldShopCollection = shopCollectionMapper.getByShopIdAndUid(shopCollection.getShopId(), loginUser.getUid());
        if(ObjectUtil.isNotNull(oldShopCollection)){
            throw new BusinessException("用户已收藏，无法再次收藏");
        }
        ShopCollection collection = new ShopCollection();
        collection.setShopId(shopCollection.getShopId());
        collection.setUserId(loginUser.getUid());
        return save(collection);
    }

    @Override
    public Boolean cancel(ShopCollection shopCollection) {
        Shop shopById = shopService.getById(shopCollection.getShopId());
        if(shopById ==null){
            throw new BusinessException("商铺信息不存在");
        }
        User loginUser = userService.getLoginUser();
        ShopCollection oldShopCollection = shopCollectionMapper.getByShopIdAndUid(shopCollection.getShopId(), loginUser.getUid());
        if(ObjectUtil.isNull(oldShopCollection)){
            throw new BusinessException("用户当前未收藏此商铺，无法取消收藏");
        }
        return removeById(oldShopCollection.getId());
    }

    @Override
    public ShopCollection getByUserIdAndShopId(Integer uid, Long shopId) {
        return shopCollectionMapper.getByShopIdAndUid(shopId,uid);
    }

    @Override
    public List<ShopVo> listShopCollectionByUid(Integer uid) {
        LambdaQueryWrapper<ShopCollection> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopCollection::getUserId, uid);
        List<ShopCollection> shopCollections = list(queryWrapper);
        if(CollUtil.isEmpty(shopCollections)){
            return Collections.emptyList();
        }
        List<Long> shopIds = shopCollections.stream().map(ShopCollection::getShopId).distinct().collect(Collectors.toList());
        return shopService.listVoByShopIds2(shopIds,uid);
    }
}




